<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Order as OrderModel;
use app\common\model\GoodsGroup as GoodsGroupModel;
use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsOption as GoodsOptionModel;
use app\common\model\GoodsGroupPerson as GoodsGroupPersonModel;
use think\Db;

/**
 * 禅旅拼团控制器
 * Class GoodsGroup
 * @package app\home\controller
 */
class Goodsgroup extends Base
{
    /**
     * [getIndex  列表]
     * @param
     * @return json
     * @author [C。]
     */
    public function Index()
    {
        $param = input('param.');
        $gpmodel = new GoodsGroupModel();
        $omodel = new OrderModel;
        $gmodel = new GoodsModel;
        $map = [];
        $map1 = [];
        $this->getPageAndSize($param);
        try {
            if(!empty($param['gotime'])){
                $goods = $gmodel->findData($param['goodsid'],'id','total');
                $gotime = strtotime($param['gotime']);
                $time = time()-15*60;
                $groupnum1 = $omodel->getCount1($param['goodsid'],$gotime,$time);
                $groupnum2 = $omodel->getCount2($param['goodsid'],$gotime);
                $groupnum = $groupnum1[0]['num'] + $groupnum2[0]['num'];
                if($groupnum >= $goods['total']){
                    $man = 1;
                }else{
                    $man = 0;
                }

                $map['o.gotime'] = strtotime($param['gotime']);
            }

            $map['o.goodsid'] = $param['goodsid'];
            $map['g.addtime'] = ['between',[time() - 24 * 3600 * config('home.cancel_time'),time() + 24 * 3600 * config('home.cancel_time')]];
            $map['go.person_num'] = ['exp',Db::raw('>g.num')];
            $map['o.status'] = 1;
            $map['o.refundid'] = ['eq',0];
            $count = $gpmodel->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $gpmodel->getList($map, $this->from, $this->size, 'o.id orderid,g.id,o.optionid oid,m.img,m.username,go.person_num,go.day,o.gotime,g.num,g.addtime');

            if(!empty($param['gotime'])){
                $map1['o.gotime'] = strtotime($param['gotime']);
                $map1['o.goodsid'] = $param['goodsid'];
                $map1['go.person_num'] = ['exp',Db::raw('=g.num')];
                $map1['o.status'] = 1;
                $map1['o.refundid'] = ['eq',0];
                $count1 = $gpmodel->getCount($map1);//满足条件的总数
                $allcount = $count + $count1;

                $list = array(
                    'count' => $count,//总记录数
                    'allpage' => $allpage,  //总页数
                    'lists' => $lists,  //列表数据
                    'allcount' => $allcount,
                    'is_man' => $man //当日团1已满 0未满
                );
            }else{
                $allcount = '';

                $list = array(
                    'count' => $count,//总记录数
                    'allpage' => $allpage,  //总页数
                    'lists' => $lists,  //列表数据
                    'allcount' => $allcount,
                );
            }
            foreach($lists as &$v){
                $v['residue'] = 24*3600*config('home.cancel_time') - (time() - $v['addtime']);
                $v['gotime'] = date('Y-m-d',$v['gotime']);
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$v['img'];
                }
                $map2['status'] = 0;
                $map2['create_time'] = ['gt',time()-15*60];
                $num = $omodel->getSum($map2,'num');
                if($v['person_num'] <= $num + $v['num']){
                    $v['isshow'] = 0;
                }else{
                    $v['isshow'] = 1;
                }
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',$list);
    }
    /**
     * [getDetial  根据拼团id获取拼团详情]
     * @param
     * @return json
     * @author [C。]
     */
    public function getDetial()
    {
        $param = input('param.');
        $model = new GoodsGroupPersonModel();
        $omodel = new OrderModel;
        $gmodel = new GoodsGroupModel;
        $gomodel = new GoodsOptionModel;
        $map = [];
        $map1 = [];
        try{
            $map['gp.group_id'] = $param['id'];
            $group_person = $model->getList($map,0,3,'m.img,gp.orderid');

            $group = $gmodel->findData($param['id'],'id','orderid,num,addtime');
            $orders = $omodel->findData($group['orderid'],'id','optionid');
            $option = $gomodel->findData($orders['optionid'],'id','person_num');

            foreach($group_person as &$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$v['img'];
                }
                $order = $omodel->findData($v['orderid'],'id','num');
                $v['num'] = $order['num'];
            }

            $map1['o.status'] = 0;
            $map1['g.id'] = $param['id'];
            $map1['o.create_time'] = ['gt',time()-10*60];
            $num = $omodel->gethassum($map1);

            $hasnum = $option['person_num'] - $group['num'] - $num;
            $residue = 24*3600 * config('home.cancel_time') - (time() - $group['addtime']);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'list'=>$group_person,
            'hasnum'=>$hasnum,
            'residue'=>$residue
        ]);
    }
}